﻿/// <reference path="jquery.min.js" />
/// <reference path="rsseasy.app.js" />

jQuery.fn.extend({
    "mapjson": function (json) {
        var t = $(this), tag = null;
        $.each(json, function (key, val) {
            tag = t.find('[' + key + ']');
            if (tag.length) {
                var length = parseInt(tag.attr("len")) || 0;
                if (length && val.length > length) {
                    val = val.substr(0, length);
                }
                switch (tag.get(0).tagName.toLowerCase()) {
                    case "input":
                    case "textarea":
                        tag.val(val);
                        break;
                    case "img":
                        tag.attr("src", RssApp.UpHost + val);
                        break;
                    case "select":
                        break;
                    default:
                        tag.html(val);
                        break;
                }
            }
        });
        t.trigger("mapjson");
        return t;
    },
    "mapitem": function (json,isappend) {
        var t = $(this);
        if (!t.data("rsstemp"))
        {
            t.data("rsstemp", t.html());
        }
        t.empty();
        $.each(json, function (idx, rows) {
            var el = $(t.data("rsstemp"));
            el.mapjson(rows);
            t.append(el);
            var dataid = el.attr("dataid");
            if (dataid) {
                el.attr("dataid", rows[dataid]);
            }
            el.show();
            t.trigger("rowsmapjson", el);
        });
        t.trigger("mapitem");
        return t;
    },
    "mapselect": function (json) {
        var t = $(this), value = t.attr("value"), text = t.attr("text"), html = "";
        $.each(json, function (idx, rows) {
            html += '<option value="' + rows[value] + '">' + rows[text] + '</option>'
        });
        t.html(html);
        return t;
    },
    "mapradio": function (json, wrap) {
        var t = $(this), name = t.attr("name"), value = t.attr("value"), text = t.attr("text"), html = "";
        if (wrap) {
            $.each(json, function (idx, rows) {
                html += '<label for="' + name + rows[value] + '"><input type="radio" id="' + name + rows[value] + '" name="' + name + '" value="' + rows[value] + '" />' + rows[text] + '</label>'
            });
        }
        else {
            $.each(json, function (idx, rows) {
                html += '<input type="radio" id="' + name + rows[value] + '" name="' + name + '" value="' + rows[value] + '" /><label for="' + name + rows[value] + '">' + rows[text] + '</label>'
            });
        }
        t.html(html);
        return t;
    },
    "mapcheckbox": function (json, wrap) {
        var t = $(this), name = t.attr("name"), value = t.attr("value"), text = t.attr("text"), html = "";
        if (wrap) {
            $.each(json, function (idx, rows) {
                html += '<label for="' + name + rows[value] + '"><input type="checkbox" id="' + name + rows[value] + '" name="' + name + '" value="' + rows[value] + '" />' + rows[text] + '</label>'
            });
        }
        else {
            $.each(json, function (idx, rows) {
                html += '<input type="checkbox" id="' + name + rows[value] + '" name="' + name + '" value="' + rows[value] + '" /><label for="' + name + rows[value] + '">' + rows[text] + '</label>'
            });
        }
        t.html(html);
        return t;
    }
});